package com.guigu.qingcheng.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    // 队列名
    public static final String FANOUT_QUEUE_NAME="queue.skuback";

    public static final String ORDER_OUT_TIME="queue.ordersj";

    //3600000
    public static long outTime=3600000;

    @Bean
    public Queue createFanoutQueue(){
        return new Queue(FANOUT_QUEUE_NAME);
    }

    @Bean
    public Queue createOrderOutTime(){
        return new Queue(ORDER_OUT_TIME);
    }

    //=============延迟消息==================
    // 创建交换机
    @Bean
    public TopicExchange orderExchange(){
        return new TopicExchange("exchenge.ordertimeout");
    }
    //定义一个接受延迟消息的队列
    @Bean
    public Queue queue(){
        Queue queue=new Queue("queue.ordertimeout",true);
        return queue;
    }
    //绑定这个队列以及这个交换机(bindingKey=queue.ordertimeout.key)
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(orderExchange()).with("queue.ordertimeout.key");
    }


    // 创建发送延迟消息的队列
    @Bean
    public Queue queueOrder(){
        Map<String,Object> map=new HashMap<>();
        map.put("x-message-ttl", outTime);// TTL 延迟时间
        map.put("x-dead-letter-exchange","exchenge.ordertimeout"); //绑定该队列的死信交换器(DXL)
        map.put("x-dead-letter-routing-key", "queue.ordertimeout.key");//路由key DLK
        Queue queue=new Queue("quque.order",true,false,false,map);
        return queue;
    }


}
